﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Web.Script.Serialization;
using VQWEB.administrator.sources;
using VQData;
using VQWEB.common;
using System.Reflection;

namespace VQWEB.administrator.handlers
{
    /// <summary>
    /// Summary description for DoanhnghiepHandler
    /// </summary>
    public class hn_DoanhnghiepHandler : IHttpHandler, IRouteHandler
    {
        JavaScriptSerializer serializer = new JavaScriptSerializer();
        public void ProcessRequest(HttpContext context)
        {
            #region Doanh Nghiep
            string add_doanhnghiep = context.Request["add_doanhnghiep"];
            if (!string.IsNullOrEmpty(add_doanhnghiep))
            {
                BusinessLayer<HuongNghiepModelDataContext> bussiness = new BusinessLayer<HuongNghiepModelDataContext>();
                var obj = bussiness.InsertOrUpdate<hn_doanhnghiep>(add_doanhnghiep);
                if (obj.chuyennganh_array_id != null)
                    bussiness.InsertAllInOne<hn_chuyennganh_doanhnghiep>(obj.chuyennganh_array_id, "chuyennganh_id", "doanhnghiep_id", obj.doanhnghiep_id);
                context.Response.Write("Thực thi thành công !");
            }
            string view_doanhnghiep = context.Request["view_doanhnghiep"];
            if (!string.IsNullOrEmpty(view_doanhnghiep))
            {
                string Id = context.Request["Id"];
                if (!string.IsNullOrEmpty(Id))
                    RenderSingle(context, Convert.ToInt32(Id));
                else
                    RenderChuyennganh(context);
            }
            string list_doanhnghiep = context.Request["list_doanhnghiep"];
            if (!string.IsNullOrEmpty(list_doanhnghiep))
            {
               RenderList(context);
            }

            string delete_doanhnghiep = context.Request["delete_doanhnghiep"];
            if (!string.IsNullOrEmpty(delete_doanhnghiep))
            {
                 BusinessLayer<HuongNghiepModelDataContext> bussiness = new BusinessLayer<HuongNghiepModelDataContext>();
                 bussiness.Delete<hn_doanhnghiep>(delete_doanhnghiep);
                RenderList(context);
            }

            string lockunlock_doanhnghiep = context.Request["lockunlock_doanhnghiep"];
            if (!string.IsNullOrEmpty(lockunlock_doanhnghiep))
            {
                BusinessLayer<HuongNghiepModelDataContext> bussiness = new BusinessLayer<HuongNghiepModelDataContext>();
                bussiness.LockUnLock<hn_doanhnghiep>(lockunlock_doanhnghiep, "doanhnghiep_active");
                RenderList(context);
            }
            #endregion
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        private void RenderChuyennganh(HttpContext context)
        {
            BusinessLayer<HuongNghiepModelDataContext> business = new BusinessLayer<HuongNghiepModelDataContext>();
            var chuyennganh = business.RenderArrayCheckboxJSON(HuongNghiepData.ViewChuyennganh(), null,"id", "ten", "chuyennganh_array_id[]");

            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("chuyennganh", chuyennganh);

            string json = serializer.Serialize(dic);
            context.Request.ContentType = "application/json";
            context.Response.Write(json);
        }
        private void RenderSingle(HttpContext context, int Id)
        {
            BusinessLayer<HuongNghiepModelDataContext> bussiness = new BusinessLayer<HuongNghiepModelDataContext>();
            object obj = bussiness.Get<hn_doanhnghiep>(
               dn => dn.doanhnghiep_id.Equals(Id),
               dn => new
               {
                   id =dn.doanhnghiep_id,
                   ten=dn.doanhnghiep_ten,
                   gioithieu=dn.doanhnghiep_gioithieu,
                   logo=dn.doanhnghiep_logo,
                   diachi=dn.doanhnghiep_diachi,
                   website=dn.doanhnghiep_website,
                   email=dn.doanhnghiep_email,
                   dienthoai=dn.doanhnghiep_dienthoai,
                   hit=dn.doanhnghiep_hit,
                   ishome=dn.doanhnghiep_ishome,
                   active = dn.doanhnghiep_active
               });


            PropertyInfo info = obj.GetType().GetProperty("id");
            var value = info.GetValue(obj, null);

            List<object> array_chuyennganh_id = bussiness.List<hn_chuyennganh_doanhnghiep>(
                null,
                null,
                n => n.doanhnghiep_id.Equals(value),
                null,
                n => n.chuyennganh_id
                ).ToList();
            List<Dictionary<string, object>> listchuyennganh = bussiness.RenderArrayCheckboxJSON(HuongNghiepData.ViewChuyennganh(), array_chuyennganh_id, "id", "ten", "chuyennganh_array_id[]");
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("doanhnghiep", obj);
            dic.Add("chuyennganh", listchuyennganh);
            string json = serializer.Serialize(dic);
            context.Request.ContentType = "application/json";
            context.Response.Write(json);
        }
        private void RenderList(HttpContext context)
        {
            BusinessLayer<HuongNghiepModelDataContext> bussiness = new BusinessLayer<HuongNghiepModelDataContext>();
            IList<object> list = bussiness.List<hn_doanhnghiep>(
                null,
                null,
                null,
                null,
               dn => new
               {
                   id = dn.doanhnghiep_id,
                   ten = dn.doanhnghiep_ten,
                   gioithieu = dn.doanhnghiep_gioithieu,
                   logo = dn.doanhnghiep_logo,
                   diachi = dn.doanhnghiep_diachi,
                   website = dn.doanhnghiep_website,
                   email = dn.doanhnghiep_email,
                   dienthoai = dn.doanhnghiep_dienthoai,
                   hit = dn.doanhnghiep_hit,
                   ishome = dn.doanhnghiep_ishome,
                   active = dn.doanhnghiep_active
               });
            string json = serializer.Serialize(list);
            context.Request.ContentType = "aplication/json";
            context.Response.Write(json);

        }

        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            return this;
        }
    }
}